<?php

namespace App\Http\Controllers;

use App\Models\Sns;
use App\Models\SnsPic;
use App\Models\SnsVideo;
use Carbon\Carbon;
use Illuminate\Http\Request;
use DB;

class SnsController extends Controller
{
  /**
   * Display a listing of the resource.
   *
   * @return \Illuminate\Http\Response
   */
  public function index(Request $request)
  {
    $sns = Sns::with(['deviceGroups', 'snsPics', 'snsVideo'])
      ->where('content', 'like', '%' . $request->input('content') . '%')
      ->orderBy('created_at', 'desc')
      ->paginate(8)
      ->toArray();
    return $this->success(0, '', $sns);
  }

  /**
   * Store a newly created resource in storage.
   *
   * @param  \Illuminate\Http\Request $request
   * @return \Illuminate\Http\Response
   */
  public function store(Request $request)
  {
    $sns = new Sns();
    $sns->content = $request->input('content');
    $sns->publish_start_time = $request->publish_start_time;
    $sns->publish_end_time = $request->publish_end_time;

    $sns->save();
    $sns->deviceGroups()->sync($request->device_group_id, false);
    if($postPics = $request->pics) {
      if (is_array($postPics) && !empty($postPics)) {
        $pics = [];
        foreach ($postPics as $key => $postPic) {
          $postPic = json_decode($postPic, true);
          $pics[$key]['name'] = $postPic['name'];
          $pics[$key]['url'] = $postPic['url'];
        }
        $sns->snsPics()->createMany($pics);
      }
    }
    if($video = $request->video) {
      $video = json_decode($request->video, true);
      if (is_array($video) && !empty($video)) {
        $snsVideo = $sns->snsVideo()->create([
          'video_name' => $video['name'],
          'video_url' => $video['url'],
        ]);
        $sns->sns_video_id = $snsVideo->id;
        $sns->save();
      }
    }

    return $this->success(0, '操作成功');
  }

  public function update(Request $request)
  {
      $sns = Sns::find($request->id);
      $sns->content = $request->input('content');
      $sns->publish_start_time = $request->publish_start_time;
      $sns->publish_end_time = $request->publish_end_time;
      $sns->save();
      DB::delete('delete from device_group_sns where sns_id = ?',[$request->id]);
      $sns->deviceGroups()->sync($request->device_group_id, false);
      if($request->type == 1){
          SnsVideo::where('sns_id',$request->id)->delete();
          $postPics = $request->pics;
          if (is_array($postPics) && !empty($postPics)) {
              $pics = [];
              foreach ($postPics as $key => $postPic) {
                  $postPic = json_decode($postPic, true);
                  $pics[$key]['name'] = $postPic['name'];
                  $pics[$key]['url'] = $postPic['url'];
              }
              $sns->snsPics()->createMany($pics);
          }
      }else{
          SnsPic::where('sns_id',$request->id)->delete();
          Sns::where('sns_video_id',$request->id)->update(['sns_video_id' => null]);
          if($video = $request->video) {
              $video = json_decode($request->video, true);
              if (is_array($video) && !empty($video)) {
                  $snsVideo = $sns->snsVideo()->create([
                      'video_name' => $video['name'],
                      'video_url' => $video['url'],
                  ]);
                  $sns->sns_video_id = $snsVideo->id;
                  $sns->save();
              }
          }
      }
      return $this->success(0, '操作成功');
  }

  /**
   * Remove the specified resource from storage.
   *
   * @param  int $id
   * @return \Illuminate\Http\Response
   */
  public function destroy($id)
  {
    $pics = Sns::find($id)->snsPics->toArray();
    // 删除上传的图片
    if(is_array($pics) && !empty($pics)) {
      $destinationPath = 'uploads/images/';
      foreach ($pics as $pic) {
        $path = public_path() . '/' . $destinationPath . $pic['name'];
        $this->deleteFromUploads($path);
      }
    }
    // 删除上传的视频
    $video = Sns::find($id)->snsVideo;
    if($video) {
      $destinationPath = 'uploads/videos/';
      $path = public_path() . '/' . $destinationPath . $video->video_name;
      $this->deleteFromUploads($path);
    }
    Sns::find($id)->snsVideo()->delete();
    Sns::find($id)->snsPics()->delete();
    Sns::destroy($id);
    return $this->success(0, '删除成功！');
  }

  public static function postedDevice($request)
  {
    $sns = Sns::findOrFail($request->sns_id);
    $device_id = $request->device_id;
    $sns->postedDevices()->sync(compact('device_id'), false);
  }
}
